package com.gec.controller;

import com.gec.model.system.SysMenu;
import com.gec.model.vo.AssginMenuVo;
import com.gec.service.SysMenuService;
import com.gec.system.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理控制器")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询全部菜单操作")
    @GetMapping("/findAllMenu")
    public Result findAllMenus(){
        List<SysMenu> sysMenuList=this.sysMenuService.findAllMenus();
        return  Result.ok(sysMenuList);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        //因为菜单下面还有子菜单
        //this.sysMenuService.removeById(id);
        this.sysMenuService.removeMenuById(id);
        return Result.ok();
    }

    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody SysMenu sysMenu){
        boolean save = sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @ApiOperation("先根据id查询菜单")
    @GetMapping("/findMenuById/{id}")
    public Result findMenuById(@PathVariable Long id){
        SysMenu byId = this.sysMenuService.getById(id);
        return  Result.ok(byId);
    }

    @ApiOperation("修改菜单")
    @PostMapping("/updateMenu")
    public Result updateMenu(@RequestBody SysMenu sysMenu){
        this.sysMenuService.updateById(sysMenu);
        return Result.ok();
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        List<SysMenu>sysMenuList=this.sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(sysMenuList);
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }

//    // 根据角色分配菜单
//    @ApiOperation("根据角色获取菜单")
//    @GetMapping("/toAssign/{roleId}")
//    public Result toAssign(@PathVariable Long roleId){
//        List<SysMenu> list =   this.sysMenuService.findSysMenuByRoleId(roleId);
//        return Result.ok(list);
//    }
//    @ApiOperation(value = "给角色分配权限")
//    @PostMapping("/doAssign")
//    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
//        sysMenuService.doAssign(assginMenuVo);
//        return Result.ok();
//    }

}
